﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using System.Xml;
using System.Collections;
using FooFun.DataElements.Dtos;

namespace PhysicalDataProvider
{
    class TopicTableDataElement : CustomDataElement
    {
        public TopicTableDataElement(IDataElement tableDE)
            : base(tableDE)
        {

        }

        public override IEnumerator GetEnumerator()
        {
            foreach (IDataElement de in InnerDataElement)
            {
                yield return new TopicDataElement(de);
            }
        }
    }

    class TopicDataElement : CustomDataElement
    {
        public TopicDataElement(IDataElement dbDE)
            : base(dbDE)
        {
            string content = (string)dbDE["Content"].Value;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(content);

            XmlElementDataElement de
                = new XmlElementDataElement(doc.DocumentElement);

            base.InnerDataElement = de;
        }

        public override IDataElement this[string attrName]
        {
            get
            {
                switch (attrName)
                {
                    // 回答次数
                    case "ReplyCount":
                        int count = 0;
                        IDataElement de = InnerDataElement["Replys"];
                        if (!de.IsEmpty)
                        {
                            IDataElement contents = de["Content"];
                            foreach (IDataElement content in contents)
                            {
                                count++;
                            }
                        }

                        return new IntDataElement(count);
                    default:
                        break;
                }
                return base[attrName];
            }
            set
            {
                base[attrName] = value;
            }
        }
    }

    class TopicDbDataElement : CustomDataElement
    {
        public TopicDbDataElement(IDataElement objDE)
            : base(objDE)
        {

        }

        public override IDataElement this[string attrName]
        {
            get
            {
                switch (attrName)
                {
                    case "TopicId":
                        return DataProviderUtil.GetGuidBase64(base["@TopicId"]);
                    case "Title":
                        return base["Title/Content"];
                    case "EventId":
                        return DataProviderUtil.GetGuidBase64(base["@EventId"]);

                    case "Status":
                        TopicStatus status =
                            (TopicStatus)Enum.Parse(typeof(TopicStatus), base["@Status"].ToString(), true);

                        return new IntDataElement((int)status);

                    case "Content":
                        XmlElementDataElement de
            = base.InnerDataElement as XmlElementDataElement;

                        XmlWriterSettings settings = new XmlWriterSettings();
                        settings.Indent = true;
                        settings.Encoding = Encoding.UTF8;

                        StringBuilder sb = new StringBuilder();
                        XmlWriter xmlWriter = XmlWriter.Create(sb);

                        de.Document.Save(xmlWriter);

                        xmlWriter.Close();

                        string content = sb.ToString();
                        return new StringDataElement(content);

                    case "ForumId":
                        return DataProviderUtil.GetGuidBase64(base["@ForumId"]);
                    default:
                        break;
                }

                return base[attrName];
            }
            set
            {
                base[attrName] = value;
            }
        }
    }
}
